package cn.yh.ssm.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import cn.yh.ssm.model.Page;
import cn.yh.ssm.model.Result;
import cn.yh.ssm.model.Student;
import cn.yh.ssm.service.StudentService;

@RestController
@RequestMapping("/student")
public class StudentController {

	@Autowired
	private StudentService studentService;
	
	/**
	 * 编辑查询
	 * @param student
	 * @return
	 */
	@RequestMapping("/findStudent")
	public ModelAndView findStudent(Student student){
		Student stu=studentService.getById(student.getId());
		Map<String,Object> map =new HashMap<String ,Object>();
		map.put("student", stu);
		return new ModelAndView("user_edit",map);
	}
	
	/**
	 * 模糊分页查询
	 * @param curPageNum
	 * @param pageSize
	 * @param name
	 * @return
	 */
	@RequestMapping(value="/findLikeStudent",method=RequestMethod.GET)
	public Page findLikeStudent(@RequestParam(required=false,defaultValue="1") int curPageNum,@RequestParam(required=false,defaultValue="3") int pageSize,@RequestParam(required=false) String name){
		Map<String , Object> map=new HashMap<String,Object>();
		map.put("curPageNum", curPageNum);
		map.put("pageSize", pageSize);
		map.put("name", name);
		return studentService.getLikeStudentByName(map);
		 
	}
	

	/**
	 * 添加student
	 * @param student
	 */
	@RequestMapping(value="/addStudent",method=RequestMethod.POST)
	public Result addStudent(Student student){
		studentService.addStudent(student);
		
		return new Result();
	}
	/**
	 * 删除student
	 * @param student
	 */
	@RequestMapping("/deleteStudent")
	public Result deleteStudent(Student student){
		studentService.deleteStudent(student);
		
		return new Result();
	}
	/**
	 * 修改student
	 */
	@RequestMapping(value="/updateStudent",method=RequestMethod.POST)
	public Result updateStudent(Student student){
		studentService.updateStudent(student);
		
		return new Result();
	}
	
}
